#include "drv_tim_pwm.h"

//呼吸灯
void drv_tim13_pwm_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);

	//配置GPIO    PF8  LED
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	
	GPIO_Init(GPIOF, &GPIO_InitStruct);
	GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_TIM13);//指定引脚PF8复用的具体功能为TIM13

	//配置定时器
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//TIM_CKD_DIV1表示不分频
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
	TIM_TimeBaseInitStruct.TIM_Period=5000-1;//从0开始计数 重装载值ARR
	TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;//从0开始计数 预分频器PSC 

	TIM_TimeBaseInit(TIM13,&TIM_TimeBaseInitStruct);
	
	//配置定时器通道
	TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;//输出模式为PWM1
	TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;//灯有效电平为低电平
	TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;//使能输出
	TIM_OCInitStruct.TIM_Pulse=0;//设置比较初值
	TIM_OC1Init(TIM13, &TIM_OCInitStruct);

	TIM_ARRPreloadConfig(TIM13, ENABLE);//开启自动重载寄存器的影子寄存器
	TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);//开启捕捉/比较寄存器的影子寄存器
	TIM_CtrlPWMOutputs(TIM13, ENABLE);//使能pwm输出
	
	TIM_Cmd(TIM13,ENABLE);//使能定时器，开始计数
}
